package com.etl.webservicenet.util;

import android.annotation.SuppressLint;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public abstract class StringUtil {

    private static String FOR_RND_STRING;
    private static Random random = new Random(System.currentTimeMillis());

    static {
        StringBuilder builder = new StringBuilder();
        for (char ch = 'A'; ch <= 'Z'; ch++) {
            builder.append(ch);
        }
        for (char ch = 'a'; ch <= 'z'; ch++) {
            builder.append(ch);
        }
        for (char ch = '0'; ch <= '9'; ch++) {
            builder.append(ch);
        }
        FOR_RND_STRING = builder.toString();
    }

    /**
     * The Levenshtein Distance Return the edit distance between strings;
     * 
     * @param source
     * @param target
     * @return the distance meaning the degree of similarity, 0 means same
     *         string;
     */
    public static int ldistance(String source, String target) {
        int m = source.length();
        int n = target.length();
        if (m == 0)
            return n;
        if (n == 0)
            return m;

        int[][] matrix = new int[m + 1][n + 1];

        for (int i = 0; i <= m; i++)
            matrix[i][0] = i;
        for (int i = 0; i <= n; i++)
            matrix[0][i] = i;

        for (int i = 1; i <= m; i++) {
            char si = source.charAt(i - 1);
            for (int j = 1; j <= n; j++) {
                char dj = target.charAt(j - 1);

                int cost = si == dj ? 0 : 1;

                int above = matrix[i - 1][j] + 1;
                int left = matrix[i][j - 1] + 1;
                int diag = matrix[i - 1][j - 1] + cost;
                matrix[i][j] = Math.min(above, Math.min(left, diag));
            }
        }

        return matrix[m][n];
    }

    /**
     * 字符串详细度，数值越大越近似
     * 
     * @param src
     * @param tar
     * @return 相似程度，一个浮点值
     */
    public static double similarity(String src, String tar) {
        int ld = ldistance(src, tar);
        return 1 - (double) ld / Math.max(src.length(), tar.length());
    }

    public static String removeEnd(String value, String end) {
        if (value.endsWith(end)) {
            int index = value.lastIndexOf(end);
            return value.substring(0, index);
        }
        return value;
    }

    public static boolean isEmpty(String string) {
        return string == null || string.length() == 0;
    }

    public static String randomString(int len) {
        StringBuilder builder = new StringBuilder();
        int length = FOR_RND_STRING.length();
        for (int i = 0; i < len; i++) {
            int index = random.nextInt(length);
            char ch = FOR_RND_STRING.charAt(index);
            builder.append(ch);
        }
        return builder.toString();
    }

    public static String Md5(String content) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(content.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            System.out.println("result: " + buf.toString());// 32位的加密
            return buf.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return content;
    }

    @SuppressLint("SimpleDateFormat")
    public static String formatDate(final String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            Date date = new Date(sdf.parse(dateStr).getTime());
            sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateStr;
    }

    
    public static String getDateStr(String dateTimeString){
    	String result = dateTimeString;
    	int nIndex = dateTimeString.indexOf('T');
    	if (nIndex < 0) {
    		return dateTimeString;
    	}
    	result = dateTimeString.substring(0, nIndex);
    	return result;
    }

    @SuppressLint("SimpleDateFormat")
    public static Date str2Date(final String dateStr) {
        if (dateStr == null || dateStr.equals("")) {
            return new Date();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = new Date(sdf.parse(dateStr).getTime());
            return date;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Date();
    }

}
